// 导出 elsx
import * as XLSX from 'xlsx'
import {saveAs} from 'file-saver'


const handleExportTableData=(elId:string,downloadName?:string |null,type?:string)=>{
    let wb = XLSX.utils.table_to_book(document.getElementById(elId), {
      raw: true // 表格数据导出execl
    }) as any
    
   
    if(type==='blob'){
        const wboot=XLSX.write(wb,{
            bookType:'xlsx',
            bookSST:true,
            type:'array'
        })
            // console.log(wb)
        const blob=new Blob ([wboot],{type:'application/octet-stream'})
        saveAs(blob,'表格数据.xlsx')
    }else{
        XLSX.writeFile(wb,downloadName+'.xlsx')
    }

   
    // 2.种方法
  }
  // 下载模块
  const handleDownloadFile=(url:string,name:string):void=>{
    let a=document.createElement('a')
    a.href=url
    a.download=name
    a.click()
  }
  // 输出指定范围内的字符
  const getENStr = (end: string) => {
    const arr = []
    for (let i = 65; i< 91; i++) {
      let code = String.fromCharCode(i)
      arr.push(code)
      if (end === code) {
       break
      }
    }
    return arr
 }
  
  const handleDownloadFileBlob = async (res:any,downloadName:string) => {
  // 以下代码可封装 res, downloadName
  let blob = new Blob([res], {type: 'application/vnd.ms-excel'});
  let url= URL.createObjectURL(blob);
  console.log('blog', blob,url)
  let a = document.createElement('a')
    a.href = url
    a.download = downloadName
    a.click()
  URL.revokeObjectURL(url)

}
  export {
    handleExportTableData,
    handleDownloadFileBlob,
    handleDownloadFile,
    getENStr
  }